package xcmg.device.service.basic;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.PurchaseDictionaryDO;
import xcmg.device.dao.mapper.PurchaseDictionaryMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.basic.PurchaseDictionaryVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;

import java.util.List;


/**
 * 采购方式service类
 * @Author huhuawei
 */
@Service
public class PurchaseDictionaryService {
    @Autowired
    private PurchaseDictionaryMapper purchaseDictionaryMapper;

    @Autowired
    private FastGenClient fastGenClient;
    /**
     * 查询
     * @param purchaseDictionaryVO
     * @return
     */
    public List<PurchaseDictionaryDO> list(PurchaseDictionaryVO purchaseDictionaryVO){
        //设置排序
        if(StringUtils.isEmpty(purchaseDictionaryVO.getpId())){
            //一级节点默认pid为0
            purchaseDictionaryVO.setpId("0");
        }
        PageCommonVO result=new PageCommonVO();
        List<PurchaseDictionaryDO> doList=purchaseDictionaryMapper.list(purchaseDictionaryVO);
        for(PurchaseDictionaryDO dic: doList){
            PurchaseDictionaryVO record=new PurchaseDictionaryVO();
            record.setpId(dic.getId());
            int count=purchaseDictionaryMapper.countByPid(record);
            dic.setChildCount(count);
        }
        return doList;
    }

    /**
     * 新增
     * @param model
     * @return
     */
    public ErrorCode add(PurchaseDictionaryVO model){
        //根据code orgid 查询当前字典是否存在
        if(purchaseDictionaryMapper.count(model)>0){
               return ErrorCode.PurchaseDataDictionaryIsExists;
        }
        if("1".equals(model.getCode()) || "2".equals(model.getCode())){
            return ErrorCode.PurchaseDataDictionaryIsExists;
        }
        ActionResult<String> textResult = fastGenClient.textGuid();
        model.setId(textResult.getValue());
        //新增的数据字典默认有效
        model.setValid((short)1);
        //判断结果
        if(purchaseDictionaryMapper.add(model)==0){
            return ErrorCode.Failure;
        }
        return ErrorCode.Success;
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    public ErrorCode remove(List<String> ids){
        if(purchaseDictionaryMapper.remove(ids)==0){
            return ErrorCode.Failure;
        }
        return ErrorCode.Success;
    }

    /**
     * 更新
     * @param model
     * @return
     */
    public ErrorCode update(PurchaseDictionaryVO model){
        //根据code orgid 查询当前字典是否存在
        if(purchaseDictionaryMapper.count(model)>0){
            return ErrorCode.PurchaseDataDictionaryIsExists;
        }
        if("1".equals(model.getCode()) || "2".equals(model.getCode())){
            return ErrorCode.PurchaseDataDictionaryIsExists;
        }
        if(purchaseDictionaryMapper.update(model)==0){
            return ErrorCode.Failure;
        }
        return ErrorCode.Success;
    }

    /**
     * 单条记录查询
     * @param id
     * @return
     */
    public PurchaseDictionaryVO item(String id){
        PurchaseDictionaryDO entity=purchaseDictionaryMapper.item(id);
        if(entity==null){
            return null;
        }
        PurchaseDictionaryVO model=new PurchaseDictionaryVO();
        BeanUtils.copyProperties(entity,model);
        return model;
    }
}
